<?php
class StepsController extends AppController {

	var $name = 'Steps';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Step->recursive = 0;
		$this->set('steps', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Step', true), array('action'=>'index'));
		}
		$this->set('step', $this->Step->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Step->create();
			if ($this->Step->save($this->data)) {
				$this->flash(__('Step saved.', true), array('action'=>'index'));
				$this->_stop();
			} else {
			}
		}
		$procedures = $this->Step->Procedure->find('list', array ('fields' => array('Procedure.proc_index', 'Procedure.proc_reference')));
		$this->set(compact('procedures'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->flash(__('Invalid Step', true), array('action'=>'index'));
			$this->_stop();
		}
		if (!empty($this->data)) {
			if ($this->Step->save($this->data)) {
				$this->flash(__('The Step has been saved.', true), array('action'=>'index'));
				$this->_stop();
			} else {
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Step->read(null, $id);
		}
		$procedures = $this->Step->Procedure->find('list', array ('fields' => array('Procedure.proc_index', 'Procedure.proc_reference')));
		$this->set(compact('procedures'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Step', true), array('action'=>'index'));
		}
		if ($this->Step->del($id)) {
			$this->flash(__('Step deleted', true), array('action'=>'index'));
		}
	}
	
//	function link(){
//		$idCat = utf8_decode($this->data['Step']['r_category']) ;
//		$cat_init = $this->Requirement->Category->find('first', array ('fields' => array('Category.cat_index')));
//		$this->set('cat_init', $cat_init) ; 
//
//		if ( $idCat == null) {
//			$idCat = $cat_init['Category']['cat_index'];
//		}
//		$this->set('categories', $this->Requirement->Category->find('list', array ('fields' => array('Category.cat_index', 'Category.cat_name'))));
//		$this->set('requirements', $this->Requirement->find('list', array ('fields' => array('Requirement.r_index', 'Requirement.r_reference'), 'conditions' => array('r_category' => $idCat)), null, null));
//		$this->set('procedures', $this->Procedure->find('list', array ('fields' => array('Procedure.proc_index', 'Procedure.proc_reference'), 'conditions' => array('proc_cat' => $idCat))));
//	}
	
	function link(){
		//		Récupération de la catégorie :
		$idCat = utf8_decode($this->data['Requirement']['r_category']) ;
		//		S'il n'y a pas de catégorie choisie (1er chargement), mise à défaut :
		if ( $idCat == null) {
			$cat_init = $this->Requirement->Category->find('first', array ('fields' => array('Category.cat_index')));
			$idCat = $cat_init['Category']['cat_index'];
		}

		$procedure = null;
		//		Si une procédure a été sélectionnée, on récupère l'id de la procédure :
		if ( isset($this->data['Procedure']['proc_reference']) && $this->data['Procedure']['proc_reference']!= null ){
			$procedure = $this->Procedure->find('first', array ('conditions' => array('proc_reference' => $this->data['Procedure']['proc_reference'], 'proc_project' => $this->Session->read('id_projet'))));

			$this->data['Procedure']['proc_index'] = $procedure['Procedure']['proc_index'] ;
			$this->data['Procedure']['proc_reference'] = $procedure['Procedure']['proc_reference'] ;
			$this->set('proc_reference', ($procedure['Procedure']['proc_reference']));
			$this->set('proc_title', ($procedure['Procedure']['proc_title']));
			$this->set('proc_ptype', ($procedure['Procedure']['proc_ptype']));
			$this->set('proc_phase', ($procedure['Procedure']['proc_phase']));
			$this->set('proc_part', ($procedure['Procedure']['proc_part']));
			$this->set('proc_version', ($procedure['Procedure']['proc_version']));
			$this->set('walle', $procedure);
				
		}
		
		$requirementsDispos = null ;
		$requirementsAssocies = null ;
		if ($procedure != null){
			$requirementsDispos = 
				$this->Requirement->query('Select R1.r_index, R1.r_reference FROM requirements R1 WHERE R1.r_project = '.$this->Session->read('id_projet').' AND R1.r_index NOT IN ( SELECT PR.pra_requirement FROM procedure_requirements PR WHERE PR.pra_proc = '.$procedure['Procedure']['proc_index']." )");
			$requirementsAssocies = 
				$this->Requirement->query('Select RA.r_index, RA.r_reference FROM requirements RA WHERE RA.r_project = '.$this->Session->read('id_projet').' AND RA.r_index IN ( SELECT PR.pra_requirement FROM procedure_requirements PR WHERE PR.pra_proc = '.$procedure['Procedure']['proc_index']." )");
		}

//		$this->set('walle', $requirementsAssocies);
		$this->set('idCat', $idCat);
		$this->set('requirementsDispos', $requirementsDispos);
		$this->set('requirementsAssocies', $requirementsAssocies);
		
		$this->set('categories', $this->Requirement->Category->find('list', array ('fields' => array('Category.cat_index', 'Category.cat_name'))));
	}

}
?>